package com.example.ssm.controller;

import com.example.ssm.bean.User;
import com.example.ssm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created by Intellij IDEA.
 * User: JonnyJiang
 * Date: 2021/6/11
 */
@Controller
public class LoginController {

    @Resource
    UserService userService;

    /**
     * 跳转登录界面
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(){
        return "login";
    }


    /**
     * 验证登录用户的账号密码是否正确以及用户是否存在
     * @param username 用户姓名
     * @param password 用户密码
     * @param session  session对象
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String check(@RequestParam("username")String username, @RequestParam("password")String password, HttpSession session){
        User user = userService.getUserByName(username);

        if (user == null){
            session.setAttribute("msg","用户不存在！！！");
            return "redirect:login";
        }
        if (user.getPassword().equals(password)){
            session.setAttribute("msg","");
            session.setAttribute("user",user);
            return "redirect:/";
        }else{
            session.setAttribute("msg","密码不正确！！！");
            return "redirect:login";
        }
    }

    /**
     * 跳转主要页面
     * @return
     */
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String forward(){
        return "index";
    }
}
